package utils
{
	import com.greensock.TweenMax;
	
	import data.MonsterProp;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.ByteArray;
	import flash.utils.getDefinitionByName;
	
	public class GameUtils
	{
		
		public static function destoyEvent(event:Event):void
		{
			event.target.removeEventListener(event.type,arguments.callee);
		}
		
		public static function depthCopeObj(o:Object):Object{
			var byteArray:ByteArray = new ByteArray();
			byteArray.writeObject(o);
			byteArray.position = 0;
			var obj:Object = byteArray.readObject();
			return obj;
		}
		
		/**
		 * 获得x方向和y方向速度值
		 * @param p1 起始点
		 * @param p2 结束点
		 * @param speed 速度
		 * @return 
		 * 
		 */
		public static function getSpeed(p1:Point,p2:Point,speed:Number):Point{
			var radian:Number = Math.atan2(p2.y - p1.y,p2.x - p1.x);
			return new Point(Math.cos(radian)*speed, Math.sin(radian)*speed);
		}
		
		public static function getTwoPointDistance(p1:Point,p2:Point):int{
			return Math.sqrt(Math.pow(p2.x - p1.x,2)+Math.pow(p2.y - p1.y,2));
		}
		/**
		 * 获得旋转角度 
		 * @param p1 自己坐标
		 * @param p2 目标点坐标
		 * @return 
		 * 
		 */		
		public static function getRotation(p1:Point,p2:Point):int{
			return Math.atan2(p2.y - p1.y,p2.x - p1.x)*180/Math.PI+90;
		}
		
		public static function setGrayFiler(dis:DisplayObject):void{
			TweenMax.to(dis,0,{alpha:1,colorMatrixFilter:{saturation:0}});
		}
		
		public static function clearGrayFiler(dis:DisplayObject):void{
			TweenMax.to(dis,0,{alpha:1,colorMatrixFilter:{saturation:1}});
		}
		
		public static function getClsFromDomain(name:String):Class{
			return getDefinitionByName(name) as Class;
		}
		
		//			import com.youbt.vo.Point;
		//			
		import flash.geom.Point;
		private static function pointOnCubicBezier(p0:Point, p1:Point, p2:Point, p3:Point, t:Number):Point
		{
			var ax:Number, bx:Number, cx:Number, ay:Number, by:Number, cy:Number, tSquared:Number, tCubed:Number;
			
			cx = 3 * (p1.x - p0.x);
			bx = 3 * (p2.x - p1.x) - cx;
			ax = p3.x - p0.x - cx - bx;
			
			cy = 3 * (p1.y - p0.y);
			by = 3 * (p2.y - p1.y) - cy;
			ay = p3.y - p0.y - cy - by;
			
			tSquared = t * t;
			tCubed = tSquared * t;
			
			var result:Point = new Point();
			result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + p0.x;
			result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + p0.y;
			return result;
		}
		
		/**
		 * 计算贝塞尔点
		 * @param p0
		 * @param p1
		 * @param p2
		 * @param p3
		 * @param pointCount
		 * @return
		 *
		 */
		public static function ComputeBezier(p0:Point, p1:Point, p2:Point, p3:Point, pointCount:int):Array
		{
			var dt:Number;
			dt = 1 / (pointCount - 1);
			var curve:Array = [];
			for (var i:int = 0; i < pointCount; i++)
			{
				curve[i] = pointOnCubicBezier(p0, p1, p2, p3, i * dt);
			}
			return curve;
		}
		
		/**
		 * 2阶贝塞尔曲线单点计算
		 * @param p0
		 * @param p1
		 * @param p2
		 * @param t (0<t<1)
		 * @return
		 *
		 */		
		public static function point2Bezier(p0:Point, p1:Point, p2:Point, t:Number):Point
		{
			var dt:Number = 1-t;
			var result:Point = new Point();
			result.x = dt*dt*p0.x + 2*t*dt*p1.x+t*t*p2.x;
			result.y = dt*dt*p0.y + 2*t*dt*p1.y+t*t*p2.y;
			return result;
		}
		
		/**
		 *  
		 * @param p0
		 * @param p1
		 * @param p2
		 * @param pointCount
		 * @return
		 *
		 */		
		public static function cmpute2Bezier(p0:Point, p1:Point, p2:Point, pointCount:int):Array{
			var dt:Number;
			dt = 1 / (pointCount - 1);
			var curve:Array = [];
			for (var i:int = 0; i < pointCount; i++)
			{
				curve[i] = point2Bezier(p0, p1, p2, i * dt);
			}
			return curve;
		}
	}
}